package com.sunchen.asc.service.parameter.server;

import com.sunchen.asc.service.common.ResultResponse;
import com.sunchen.asc.service.common.ResultUtil;
import com.sunchen.asc.service.parameter.api.ParamService;
import com.sunchen.asc.service.parameter.api.ParamTypeService;
import com.sunchen.asc.service.parameter.domain.ParamType;
import com.sunchen.asc.service.parameter.repository.ParamTypeRepository;
import com.sunchen.asc.service.parameter.vo.ParamTypeVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.sql.Timestamp;
import java.util.List;

@DubboService(retries = 0)
public class ParamTypeServiceImpl implements ParamTypeService {

    @Autowired
    private ParamTypeRepository paramTypeRepository;

    public ResultResponse create(ParamTypeVo paramTypeVo) {

        Long countN = paramTypeRepository.countByNameAndDeleteFlag(paramTypeVo.getName(),0);
        if (countN > 0){
            return ResultUtil.failed("名称重复");
        }
        Long countV = paramTypeRepository.countByValueAndDeleteFlag(paramTypeVo.getValue(),0);
        if (countV > 0){
            return ResultUtil.failed("值重复");
        }
        ParamType paramType = new ParamType();
        paramType.setStatus(paramTypeVo.getStatus());
        paramType.setParentId(paramTypeVo.getParentId());
        paramType.setFixed(paramTypeVo.getFixed());
        paramType.setName(paramTypeVo.getName());
        paramType.setValue(paramTypeVo.getValue());
        paramType.setDeleteFlag(0);
        //paramType.setCreateUser();
        paramTypeRepository.save(paramType);

        return ResultUtil.success("1");
    }

    public ResultResponse edit(ParamTypeVo paramTypeVo) {

        ParamType paramType = paramTypeRepository.getOne(paramTypeVo.getId());

        if (!paramTypeVo.getName().equals(paramType.getName())){
            Long countN = paramTypeRepository.countByNameAndDeleteFlag(paramTypeVo.getName(),0);
            if (countN > 0){
                return ResultUtil.failed("名称重复");
            }
        }
        if (!paramTypeVo.getValue().equals(paramType.getValue())){
            Long countV = paramTypeRepository.countByValueAndDeleteFlag(paramTypeVo.getValue(),0);
            if (countV > 0){
                return ResultUtil.failed("值重复");
            }
        }
        paramType.setName(paramTypeVo.getName());
        paramType.setValue(paramTypeVo.getValue());
        //paramType.setUpdateUser();
        paramTypeRepository.save(paramType);

        return ResultUtil.success("1");
    }

    public ResultResponse list(String name) {

        List<ParamTypeVo> vos = paramTypeRepository.findVoByName("%"+name+"%");
        return ResultUtil.success(vos);
    }

    public ResultResponse status(Long id, int status) {

        if (status == 1){
            //开启
            ParamType paramType = paramTypeRepository.getOne(id);
            if (paramType.getParentId() != 0){
                ParamType father = paramTypeRepository.getOne(paramType.getParentId());
                if (father.getStatus() == 0){
                    return ResultUtil.failed("请先启用父级类型");
                }
            }
            paramType.setStatus(status);
            //paramType.setUpdateUser();
            paramType.setUpdateTime(new Timestamp(System.currentTimeMillis()));
            paramTypeRepository.save(paramType);
            return ResultUtil.success("1");
        }else {
            //关闭
            Long count = paramTypeRepository.countByDeleteFlagAndStatusAndParentId(0,1,id);
            if (count > 0){
                return ResultUtil.failed("子类型正在使用中，无法禁用");
            }
            ParamType paramType = paramTypeRepository.getOne(id);
            paramType.setStatus(status);
            //paramType.setUpdateUser();
            paramType.setUpdateTime(new Timestamp(System.currentTimeMillis()));
            paramTypeRepository.save(paramType);
            return ResultUtil.success("1");
        }
    }
}
